﻿String.prototype.stripHTML = function () {
    var reTag = /<(?:.|\s)*?>/g;
    return this.replace(reTag, "");
}


function MakeModel() {
    var models = {};
    if (typeof (model) == "object") {
        models = model;
    }
    $.each($(".tab-content [field]"), function (i, e) {
        var value = GetValues(e).trim();
        models[$(e).attr("field")] = (value == null ? "" : value);
    });
    return models;
}

function SetModel(models) {
    if (!$.isEmptyObject(models)) {
        //console.log(models);
        $.each($(".tab-content [field]"), function (i, e) {
            //models[$(e).attr("field")] = GetValues(e);
            //console.log(e, models[$(e).attr("field")]);
            if (models[$(e).attr("field")] !== undefined && models[$(e).attr("field")] != "") {
                //console.log($(e).attr("field"), models[$(e).attr("field")])
                SetValues(e, models[$(e).attr("field")]);
            }
        });
    }
}

//获取元素的值
function GetValues(e) {
    //字段类型
    var type = $(e).attr("field_type").toUpperCase();
    var values = "";
    switch (type) {
        case "INPUT":
            values = $(e).val();
            break;
        case "PASSWORD":
            values = $(e).val();
            break;
        case "PASSWORD_EYE":
            values = $(e).val();
            break;
        case "TEXTAREA":
            values = $(e).val();
            //values = encodeURIComponent("_FLAG_CONTENT_ENCODE_" + values);
            break;
        case "EDITOR":
            values = UE.getEditor("editor_" + $(e).attr("field")).getContent();
            //values = encodeURIComponent("_FLAG_CONTENT_ENCODE_" + values);
            break;
        case "FEDITOR":
            values = $(e).froalaEditor('html.get', true);
            break;
        case "RADIO":
            values = $("input[name='options_" + $(e).attr("field") + "']:checked").val();
            values = (values === undefined ? "" : values);
            break;
        case "CHECKBOX":
            $("input[name='options_" + $(e).attr("field") + "']:checked").each(function () {
                values += this.value + ',';
            });
            values = (values == "" ? "" : ',' + values);
            break;
        case "SELECT":
            values = $(e).val() == "" ? "0" : $(e).val();
            break;
        case "MULTIPLESELECT":
            if ($(e).val() !== null) {
                for (var i = 0; i < $(e).val().length; i++) {
                    values += $(e).val()[i] + ",";
                }
                values = (values == "" ? "" : ',' + values);
            }
            break;
        case "FILEUPLOAD":
            values = $(e).attr("data-value") === undefined ? "" : $(e).attr("data-value");
            break;
        case "IMGUPLOAD":
            values = $(e).attr("data-value") === undefined ? "" : $(e).attr("data-value");
            break;
        case "MULTIPLEIMGUPLOAD":
            $(".Multiple_" + $(e).attr("field") + " img").each(function () {
                values += '{"ImgUrl":"' + $(this).attr("src") + '","Thumbnail":"' + ($(this).attr("src")).replace(appConfig.uploadPath, appConfig.uploadPath + "Thumbnail/") + '","Title":"' + $(this).attr("title") + '","Desc":"' + $(this).attr("desc") + '"},';
            });
            if (values != "") {
                values = "[" + values.substring(0, values.length - 1) + "]";
            }
            values = values.replace(/\n/g, "<br/>");
            //values = encodeURIComponent("_FLAG_CONTENT_ENCODE_" + values);
            break;
        case "TIME":
            values = $(e).val();
            break;
        case "TIME_MINUTE":
            values = $(e).val();
            break;
        case "SWITCH":
            if (QueryString.GetValue("t") == "Roles") {
                values = CheckBox_IsCheck($(e)) ? 1 : 0;
            }
            else if ($(e).attr("field").toUpperCase() == "States".toUpperCase()) {
                values = CheckBox_IsCheck($(e)) ? 0 : 1;
            }
            else {
                values = CheckBox_IsCheck($(e)) ? 1 : 0;
            }
            break;
        case "LIST":
            if (JSON.stringify(fieldList_getDataJson($(e).attr("field"))) == "[]") {
                values = "";
            }
            else {
                values = JSON.stringify(fieldList_getDataJson($(e).attr("field")));
            }
            break;
        default:
            values = "";
            break;
    }
    values = StrEnCode(values);
    return (values);
}

//给元素赋值   e=元素 v=值
function SetValues(e, v) {
    v = v + ""; //隐式转换为string类型
    v = StrDeCode(v);
    //字段类型
    var type = $(e).attr("field_type").toUpperCase();
    switch (type) {
        case "INPUT":
            $(e).val(v);
            break;
        case "PASSWORD":
            $(e).val(v);
            break;
        case "PASSWORD_EYE":
            $(e).val(v);
            break;
        case "TEXTAREA":
            $(e).val(v);
            break;
        case "EDITOR":
            UE.getEditor("editor_" + $(e).attr("field")).ready(function () {
                UE.getEditor("editor_" + $(e).attr("field")).setContent(v);
            })
            break;
        case "FEDITOR":
            if (v == "") {
                v = $("#" + "feditor_default_" + $(e).attr("field")).val();
            }
            window["feditor_content_" + $(e).attr("field")] = v;
            $(e).froalaEditor("html.set", v);
            break;
        case "RADIO":
            $("input[name='options_" + $(e).attr("field") + "'][value='" + v + "']").attr("checked", 'checked');
            break;
        case "CHECKBOX":
            var values = v.split(',');
            for (var i = 1; i < values.length - 1; i++) {
                $("input[name='options_" + $(e).attr("field") + "'][value='" + values[i] + "']").attr("checked", 'checked');
            }
            break;
        case "SELECT":
            $(e).val(v).trigger("change");
            break;
        case "MULTIPLESELECT":
            var values = v.split(',');
            var val = new Array();
            for (var i = 1; i < values.length - 1; i++) {
                val.push(values[i]);
            }
            $("select[field='" + $(e).attr("field") + "']").val(val).trigger("change");
            break;
        case "FILEUPLOAD":
            $(e).attr("data-value", v);
            $("input[name=filesrc_" + $(e).attr("field") + "]").val(v);
            break;
        case "IMGUPLOAD":
            $(e).attr("data-value", v);
            var thumbnail = $(e).closest(".fileinput").children(".thumbnail");
            thumbnail.css("height", "auto");
            thumbnail.find("img").attr("src", v);
            $("input[name=imgsrc_" + $(e).attr("field") + "]").val(v);
            break;
        case "MULTIPLEIMGUPLOAD":
            //v = decodeURIComponent(v);
            if (v != "") {
                var val = JSON.parse(v);
                var MultipleImg = "";
                val.forEach(function (ele, i) {
                    MultipleImg = "";
                    MultipleImg += "<div class=\"Multiple-Img thumbnail\" data-toggle=\"context\" data-target=\"#context-menu\" >";
                    MultipleImg += "<span style=\"background-image:url(" + ele.ImgUrl + ")\">";
                    MultipleImg += "<img src=" + ele.ImgUrl + " title=\"" + ele.Title + "\" desc=\"" + ele.Desc + "\">";
                    MultipleImg += "</span>";
                    MultipleImg += "</div>";
                    $(".Multiple_" + $(e).attr("field")).append(MultipleImg);
                });
                //v = v.replace(/<br\/>/g, "\n");
                InitContextMenu();
                InitMultipleDrag();
            }
            break;
        case "TIME":
            $(e).val($.FormartDate("yyyy-MM-dd hh:mm:ss", v));
            break;
        case "TIME_MINUTE":
            $(e).val($.FormartDate("yyyy-MM-dd hh:mm", v));
            break;
        case "SWITCH":
            if (QueryString.GetValue("t") == "Roles") {
                Switch_State($(e), (parseInt(v) == 1 ? true : false));
            }
            else if ($(e).attr("field").toUpperCase() == "States".toUpperCase()) {
                Switch_State($(e), (parseInt(v) == 1 ? false : true));
            }
            else {
                Switch_State($(e), (parseInt(v) == 1 ? true : false));
            }
            break;
        case "LIST":
            fieldList_setValue($(e).attr("field"), escape(v));
            fieldList_layTable($(e).attr("field"));
            //console.log("SET值" + e);
            break;
        default:
            break;
    }
}

function AddClick() {
    window.location.href = "Edit.aspx?action=add" + QueryString.GetValue("t") + "&t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
}
function EditClick(id) {
    window.location.href = "Edit.aspx?action=edit" + QueryString.GetValue("t") + "&t=" + QueryString.GetValue("t") + "&id=" + id + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("key") === undefined ? "" : "&key=" + QueryString.GetValue("key")) + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
}

//栏目添加子分类
function AddChild(id, pid) {
    window.location.href = "Edit.aspx?action=add" + QueryString.GetValue("t") + "&t=" + QueryString.GetValue("t") + "&parentid=" + id + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
}

//添加字段
function AddField() {
    window.location.href = appConfig.adminPath + "Page_Field/List.aspx?t=Fields" + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
}

//回跳到列表
function JumpList() {
    if (QueryString.GetValue("t").toUpperCase() == "SINGLEPAGE") {
        window.location.href = "SinglePage.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("action") === undefined ? "" : "&action=" + QueryString.GetValue("action")) + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid"));
    }
    else {
        if (IsFromRecycle()) {
            window.location.href = "Recycle.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("key") === undefined ? "" : "&key=" + QueryString.GetValue("key")) + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
        } else {
            window.location.href = "List.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("key") === undefined ? "" : "&key=" + QueryString.GetValue("key")) + (QueryString.GetValue("page") === undefined ? "" : "&page=" + QueryString.GetValue("page"));
        }
    }
}

//跳转登录
function JumpLogin() {
    window.location.href = appConfig.adminPath + "login.aspx?from=" + encodeURIComponent(window.location.href);
}
//登出
function JumpLoginOut() {
    window.location.href = appConfig.adminPath + "login.aspx?action=loginout";
}
//跳转到添加字段页面
function JumpAddField(id) {
    window.location.href = appConfig.adminPath + "Page_Field/List.aspx?t=Fields&classid=" + id;
}
//跳转到模块管理页面
function JumpModuleList(id) {
    window.location.href = appConfig.adminPath + "Page_Module/List.aspx?t=Module&classid=" + id;
}
function JumpModuleEdit(id) {
    window.location.href = appConfig.adminPath + "Page_Module/Edit.aspx?t=Module&classid=" + id;
}
//系统日志分页
function JumpSystemLogPage(page) {
    window.location.href = "SystemLog.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("key") === undefined ? "" : "&key=" + QueryString.GetValue("key")) + "&page=" + page;
}
//分页
function JumpPage(page) {
    window.location.href = "List.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid")) + (QueryString.GetValue("key") === undefined ? "" : "&key=" + QueryString.GetValue("key")) + "&page=" + page;
}

//后台其他地方跳转到栏目编辑页
function JumpEditChannelClick(classId) {
    window.location.href = appConfig.adminPath + "Page_Channel/Edit.aspx?action=editChannel&t=Channel&id=" + classId;
}

function JumpRecycle()
{
    window.location.href = "Recycle.aspx?t=" + QueryString.GetValue("t") + (QueryString.GetValue("classid") === undefined ? "" : "&classid=" + QueryString.GetValue("classid"));
}

/*验证事件的调用 e=元素 r=结果*/
function VerifyHandle(e, r) {
    if (r) {
        e.removeClass("has-error");
    }
    else {
        if (e.hasClass("has-error") === false) {
            e.addClass("has-error");
        }
    }
}
//检查页面上所有输入是否全部通过检查
function CheckVerify() {
    return $(".has-error").length == 0 ? true : false;
}


/*
通用提交 返回Json 默认使用Post提交
SynSubmit.apply(this, [1,2,3,4])  
第一个参数是url
第二个参数是action
第三个参数是data值
第四个参数是Url 的param 此参数会增加到Url上面
最后一个参数是回调
*/
function AjaxSubmit() {
    var args = Array.prototype.slice.call(arguments);
    var url_param = args[0] + "?action=" + args[1];
    if (args.length > 4) {
        for (var i = 3; i < args.length - 1; i++) {
            url_param += "&" + args[i];
        }
    }
    args[2].token = csrf_token;
    $.ajax({
        type: "POST",
        url: url_param,
        data: args[2],
        dataType: "json",
        success: function (data) {
            args[args.length - 1](true, data);
        },
        error: function (xhr, textStatus, responseText) {
            //console.log(xhr, textStatus, responseText);
            args[args.length - 1](false, xhr);
        }
    });
}

//检查输入的内容
function CheckFieldData() {

}


/*自动保存*/
function InitAutoSaveInCookie() {
    setTimeout(function () {
        if (appConfig.AutoSave) {
            setInterval(function () {
                //AutoSaveName  StrEnCode
                //$.Cookie_Set(AutoSave_CookieName(), GetPageFlag() + "=" + StrEnCode(JSON.stringify(MakeModel())), "d1");  //不用Cookie  改用localStorage
                localStorage.setItem(GetPageFlag(), JSON.stringify(MakeModel()));
                notice("自动保存内容成功");
            }, appConfig.AutoSaveTime);
        }
    }, 10000);
}
/*恢复自动保存的对象*/
function InitAutoReStoryInCookie() {
    if (appConfig.AutoSave) {
        if (localStorage.getItem(GetPageFlag()) == null) {
            return false;
        }
        var AutoSaveCont = localStorage.getItem(GetPageFlag());
        if (AutoSaveCont !== null) {
            AutoSaveCont = StrDeCode(AutoSaveCont);
            SetModel(AutoSaveCont);
        }
    }
}

/*验证密码是否符合规定*/
function VerifyPwd(pwd) {
    //8-20 1大写字母 1小写字母 1数字  1特殊字符
    return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,20}$/.test(pwd);
}

$(function () {

    //编辑或者添加的时候 初始化验证
    if (IsEditAction() || IsAddAction()) {
        InitAutoReStoryInCookie();
        InitAutoSaveInCookie();
        $("div.tab-content [verify]").Verify({
            subtn: $(".submit"),
            callback: function (res, type, obj) { //res=验证结果 type=验证类型 obj=验证的对象  //当验证失败才会调用此方法
                //console.log(res, type, obj);
                VerifyHandle(obj.closest(".form-group"), res);
            },
            submit: function (res) {
                //console.log(1);
            }
        });
    }


    //提交按钮的功能 后台统一提交地址 统管编辑和添加
    $(".submit").on("click", function () {
        //if ($(this).hasClass("disabled")) {
        //    return;
        //}
        //检查一遍输入状态
        if (!CheckVerify()) {
            notice("请检查您的输入项是否有错误", "error");
            return false;
        }
        //检查必要参数
        if (GetQs("action") == "" || GetQs("t") == "") {
            alert("您的访问异常 , 请重新登录后再尝试操作", function () {
                window.location.href = appConfig.adminPath + "login.aspx?action=loginout";
            });
            return false;
        }
        var ActionType = "操作";
        if (IsAddAction()) {
            ActionType = "添加";
        }
        if (IsEditAction()) {
            ActionType = "编辑";
        }

        var _this = this;
        model = MakeModel();



        if (GetQs("t") != "Fields" && GetQs("t") != "Versions") {
            if (model.ClassId == 10 || model.ClassId == 1063) {
                if (model.SeoTitle == "" || model.SeoTitle == undefined) {
                    model.SeoTitle = model.Title + "-深圳云笔记";
                }
                if (model.SeoKeyWords == "" || model.SeoKeyWords == undefined) {
                    model.SeoKeyWords = model.Title;
                }
                if (model.SeoDescription == "" || model.SeoDescription == undefined) {

                    var devar = model.Content;
                    if (model.Content != "" && model.Content != undefined) {
                        devar = StrDeCode(devar);
                        var FiterCont = devar.stripHTML().replace(/&nbsp;/ig, "").replace(/\n/ig, "").replace(/	/ig, " ").replace(/ /ig, "")
                        model.SeoDescription = $.trim(FiterCont.substring(0, 100));
                    }
                }
            }
        }


        //加密密码
        if (model["UserPwd"] != null && model["UserPwd"] != undefined && model["UserPwd"] != "") {
            model["UserPwd"] = rsa.Encrypt(model["UserPwd"]);
        }

        var data = { model: JSON.stringify(model), table: GetQs("t"), classid: GetQs("classid") };
        if (!$(_this).hasClass("btn_submit_vertical")) {
            $(_this).button('loading');
        }
        else {
            Btn_Disabled($(_this));
        }
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", GetQs("action"), data, function (f, d) {
            $(_this).button('reset');
            Btn_UnDisabled($(_this));
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    alert(ActionType + "成功", function () {
                        //if (IsEditAction() || IsAddAction()) { $.Cookie_Del(AutoSave_CookieName()) }
                        localStorage.removeItem(AutoSave_CookieName());
                        setTimeout(function () {
                            localStorage.removeItem(AutoSave_CookieName()); //防止插入的瞬间提交 , 再次删除 以防万一
                            if (GetQs("t") == "Versions") {
                                window.location.href = window.location.href;
                            } else {
                                JumpList();
                            }
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else if (parseInt(d.result) == -9999) {
                    alert("非法操作 , 请刷新页面后重试 !");
                }
                else {
                    alert(ActionType + "失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                if (d.status == 405) {
                    alert("您提交的内容中检测到有潜在危险的信息 , 服务器已经阻止了您当前请求");
                } else {
                    //请求失败
                    alert("请求出错 , 请稍后刷新重试 !");
                }
            }
        });
    });

    //删除按钮功能 统管 删除按钮
    $(".del").on("click", function () {
        var _this = $(this);
        var msg = "";
        bootbox.dialog({
            message: "<p><h4>您确定要删除" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4></p>",
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), states: -1, table: GetQs("t"), classid: GetQs("classid") };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetState", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("删除" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("操作失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });
    });
    //锁定解锁
    $(".states").on("click", function () {
        var _this = $(this);
        var msg = "";
        if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
            msg = "<p><h4>您确定要解锁" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4> </p>";
        }
        else {
            msg = "<p><h4>您确定要锁定" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4> </p>";
        }
        bootbox.dialog({
            message: msg,
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
                        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid") };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetState", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    notice("操作成功", "success");
                                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                                        _this.attr("data-states", 1).removeClass("red").addClass("default");
                                    } else {
                                        _this.attr("data-states", 0).removeClass("default").addClass("red");
                                    }
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("操作失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });
    });
    //置顶取消置顶
    $(".istop").on("click", function () {
        var _this = $(this);
        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid") };
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetTop", data, function (f, d) {
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    notice("操作成功", "success");
                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                        _this.attr("data-states", 1).removeClass("default").addClass("red");
                    } else {
                        _this.attr("data-states", 0).removeClass("red").addClass("default");
                    }
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });
    //设置首页
    $(".isindex").on("click", function () {
        var _this = $(this);
        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid") };
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetIndex", data, function (f, d) {
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    notice("操作成功", "success");
                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                        _this.attr("data-states", 1).removeClass("default").addClass("red");
                    } else {
                        _this.attr("data-states", 0).removeClass("red").addClass("default");
                    }
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });
    //设置热门
    $(".ishot").on("click", function () {
        var _this = $(this);
        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid") };
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetHot", data, function (f, d) {
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    notice("操作成功", "success");
                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                        _this.attr("data-states", 1).removeClass("default").addClass("red");
                    } else {
                        _this.attr("data-states", 0).removeClass("red").addClass("default");
                    }
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });
    //设置推荐
    $(".isred").on("click", function () {
        var _this = $(this);
        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid") };
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetRed", data, function (f, d) {
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    notice("操作成功", "success");
                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                        _this.attr("data-states", 1).removeClass("default").addClass("red");
                    } else {
                        _this.attr("data-states", 0).removeClass("red").addClass("default");
                    }
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });

    //物理删除
    $(".real-del").on("click", function () {
        //判断是否回收站
        if (!IsRecycle()) {
            alert("仅在回收站中才可进行此操作");
            return;
        }
        var _this = $(this);
        bootbox.dialog({
            message: "<p><h4>您确定要<span class='font-red'>彻底删除</span>" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4><h5 class='font-red bold'>彻底删除后无法恢复 , 请谨慎操作</h5></p>",
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid") };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "RealDel", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    //notice("操作成功", "success");
                                    //window.location.href = window.location.href;
                                    alert("彻底删除成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("操作失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });



    });
    //恢复
    $(".restore").on("click", function () {
        //判断是否回收站
        if (!IsRecycle()) {
            alert("仅在回收站中才可进行此操作");
            return;
        }

        var _this = $(this);

        bootbox.dialog({
            message: "<p><h4>您确定要恢复" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4></p>",
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid") };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "Restore", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    notice("操作成功", "success");
                                    window.location.href = window.location.href;
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("操作失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });


        
        
    });

    //复制数据
    $(".copy").on("click", function () {
        var _this = $(this);
        var IsChannel = _this.attr("data-tip-type") == "栏目";
        bootbox.dialog({
            message: "<p><h4>您确定要复制" + _this.attr("data-tip-type") + " " + _this.attr("data-title") + " 吗 ? </h4>" + (IsChannel ? "<h5 class='font-red'>复制完栏目后 , 您有一次更改栏目访问目录的机会 , 请及时修改</h5>" : "") + "</p>",
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid") };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CopyData", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("复制" + _this.attr("data-tip-type") + "成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("复制失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });
    });

    //复制栏目
    $(".copychannel").on("click", function () {
        var _this = $(this);
        bootbox.dialog({
            message: "<p><h4>您确定要复制栏目 " + _this.attr("data-title") + " 吗 ? </h4>" + "<h5 class='font-red'>复制完栏目后 , 您有一次更改栏目访问目录的机会 , 请及时修改</h5><h5 class='font-blue-soft'>如果您需要将栏目里的内容一同复制 , 请选择 \"确定 , 包含数据\"</h5><h5 class='font-blue-soft'>如果您需要将栏目的子级栏目一同复制 , 请选择 \"确定 , 包含子级\"</h5>" + "</p>",
            title: appConfig.toastrTitle,
            buttons: {
                comfirm1: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid"), hasdata: "0", haschild: "0" };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CopyData", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("复制" + _this.attr("data-tip-type") + "成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("复制失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                comfirm2: {
                    label: "确定，包含数据",
                    className: "green-meadow",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid"), hasdata: "1", haschild: "0" };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CopyData", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("复制" + _this.attr("data-tip-type") + "成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("复制失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                comfirm3: {
                    label: "确定，包含子级",
                    className: "green-haze",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid"), hasdata: "0", haschild: "1" };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CopyData", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("复制" + _this.attr("data-tip-type") + "成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("复制失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                comfirm4: {
                    label: "确定，包含数据和子级",
                    className: "green-seagreen",
                    callback: function () {
                        Btn_Disabled(_this);
                        var data = { id: _this.attr("data-id"), table: GetQs("t"), classid: GetQs("classid"), hasdata: "1", haschild: "1" };
                        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CopyData", data, function (f, d) {
                            Btn_UnDisabled(_this);
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) > 0) {
                                    alert("复制" + _this.attr("data-tip-type") + "成功 !", function () {
                                        window.location.href = window.location.href;
                                    });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("复制失败 , 请稍后重试 !");
                                }
                            }
                            else {
                                //请求失败
                                alert("请求出错 , 请稍后刷新重试 !");
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });
    });

    //栏目快捷操作
    $(".c-quick").on("click", function () {
        var _this = $(this);
        var states = parseInt(_this.attr("data-states")) == 0 ? 1 : 0;
        var data = { id: _this.attr("data-id"), states: states, table: GetQs("t"), classid: GetQs("classid"), operat: _this.attr("data-operat") };
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "ChannelQuickOperat", data, function (f, d) {
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) > 0) {
                    notice("操作成功", "success");
                    if (parseInt(_this.attr("data-states")) == 0) { //需要解锁
                        _this.attr("data-states", 1).removeClass("default").addClass("blue-dark");
                    } else {
                        _this.attr("data-states", 0).removeClass("blue-dark").addClass("default");
                    }
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -100) {
                    alert("权限不足 , 无法操作 !");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            }
            else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });
    });

    $(".closemode").on("click", function () {
        var _this = $(this);
        var mt = _this.attr("data-mt");
        var msg = "";
        if (mt == "dev") {
            msg = "<p><h4>您确定要关闭开发者模式吗 ? </h4>如果需要重新打开 , 请联系技术重新打开 </p>";
        }
        if (mt == "module") {
            msg = "<p><h4>您确定要关闭模块开发模式吗 ? </h4>如果需要重新打开 , 请联系技术重新打开 </p>";
        }
        var flag = true; //如果是true 说明可以请求 如果是false说明正在请求 结果未反馈
        if (!flag) {
            return false;
        }
        bootbox.dialog({
            message: msg,
            title: appConfig.toastrTitle,
            buttons: {
                comfirm: {
                    label: "确定",
                    className: "green",
                    callback: function () {
                        var data = {};
                        flag = false;
                        AjaxSubmit(appConfig.adminPath + "ashx/ajax.ashx", "close" + mt + "mode", data, function (f, d) {
                            flag = true;
                            if (f) {
                                //请求成功
                                if (parseInt(d.result) == 1) {
                                    notice("操作成功", "success");
                                    $("." + mt + "modealert").hide(200, function () { $(this).remove() });
                                }
                                else if (parseInt(d.result) == -99) {
                                    alert("登录超时 , 请重新登录", function () {
                                        setTimeout(function () {
                                            JumpLogin();
                                        }, 500);
                                    });
                                }
                                else if (parseInt(d.result) == -100) {
                                    alert("权限不足 , 无法操作 !");
                                }
                                else {
                                    alert("操作失败 , 请稍后重试 !");
                                }
                            }
                        });
                    }
                },
                cancel: {
                    label: "取消",
                    className: "default",
                    callback: function () { }
                }
            }
        });

    });
});

//多选后的操作 防止变量污染
$(function () {

    //计算当前页面有多少ID被选中
    function CalcSelect() {
        var select = "";
        $(".check-one:checked").each(function () {
            select += $(this).attr("data-id") + ",";
        });
        if (select.length > 0) {
            select = select.substring(0, select.length - 1);
        }
        return select;
    }

    //多个删除 完成后刷新页面
    $(".del_select").on("click", function () {
        var ids = CalcSelect();
        if (ids == "") {
            notice("您未选择任何需要删除的数据", "warning");
        }
        else {
            var _this = $(this);
            var msg = "";
            bootbox.dialog({
                message: "<p><h4>您确定要删除选中的内容吗 ? </h4></p>",
                title: appConfig.toastrTitle,
                buttons: {
                    comfirm: {
                        label: "确定",
                        className: "green",
                        callback: function () {
                            //Btn_UnDisabled(_this);
                            //Btn_Disabled(_this);
                            _this.attr("disabled", "disabled");
                            var data = { ids: ids, table: GetQs("t"), classid: GetQs("classid") };
                            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetState_Delete", data, function (f, d) {
                                _this.removeAttr("disabled");
                                if (f) {
                                    //请求成功
                                    if (parseInt(d.result) > 0) {
                                        notice("批量删除成功", "success");
                                        setTimeout(function () { location.reload(); }, 1500);
                                    }
                                    else if (parseInt(d.result) == -99) {
                                        alert("登录超时 , 请重新登录", function () {
                                            setTimeout(function () {
                                                JumpLogin();
                                            }, 500);
                                        });
                                    }
                                    else if (parseInt(d.result) == -100) {
                                        alert("权限不足 , 无法操作 !");
                                    }
                                    else {
                                        alert("操作失败 , 请稍后重试 !");
                                    }
                                }
                                else {
                                    //请求失败
                                    alert("请求出错 , 请稍后刷新重试 !");
                                }
                            });
                        }
                    },
                    cancel: {
                        label: "取消",
                        className: "default",
                        callback: function () { }
                    }
                }
            });

            
        }

    });

    //多个锁定 完成后刷新页面
    $(".states_lock_select").on("click", function () {
        var ids = CalcSelect();
        if (ids == "") {
            notice("您未选择任何需要锁定的数据", "warning");
        }
        else {
            var _this = $(this);
            _this.attr("disabled", "disabled");
            var data = { ids: ids, table: GetQs("t"), classid: GetQs("classid") };
            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetState_Lock", data, function (f, d) {
                _this.removeAttr("disabled");
                if (f) {
                    //请求成功
                    if (parseInt(d.result) > 0) {
                        notice("批量锁定成功", "success");
                        setTimeout(function () { location.reload(); }, 1500);
                    }
                    else if (parseInt(d.result) == -99) {
                        alert("登录超时 , 请重新登录", function () {
                            setTimeout(function () {
                                JumpLogin();
                            }, 500);
                        });
                    }
                    else if (parseInt(d.result) == -100) {
                        alert("权限不足 , 无法操作 !");
                    }
                    else {
                        alert("操作失败 , 请稍后重试 !");
                    }
                }
                else {
                    //请求失败
                    alert("请求出错 , 请稍后刷新重试 !");
                }
            });

        }
        //console.log(CalcSelect());

    });

    //多个解锁 完成后刷新页面
    $(".states_unlock_select").on("click", function () {
        var ids = CalcSelect();
        if (ids == "") {
            notice("您未选择任何需要解锁的数据", "warning");
        }
        else {

            var _this = $(this);
            _this.attr("disabled", "disabled");
            var data = { ids: ids, table: GetQs("t"), classid: GetQs("classid") };
            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetState_UnLock", data, function (f, d) {
                _this.removeAttr("disabled");
                if (f) {
                    //请求成功
                    if (parseInt(d.result) > 0) {
                        notice("批量解锁成功", "success");
                        setTimeout(function () { location.reload(); }, 1500);
                    }
                    else if (parseInt(d.result) == -99) {
                        alert("登录超时 , 请重新登录", function () {
                            setTimeout(function () {
                                JumpLogin();
                            }, 500);
                        });
                    }
                    else if (parseInt(d.result) == -100) {
                        alert("权限不足 , 无法操作 !");
                    }
                    else {
                        alert("操作失败 , 请稍后重试 !");
                    }
                }
                else {
                    //请求失败
                    alert("请求出错 , 请稍后刷新重试 !");
                }
            });

        }
        
    });


    //多个物理删除 物理删除
    $(".real-dels").on("click", function () {
        //判断是否回收站
        if (!IsRecycle()) {
            alert("仅在回收站中才可进行此操作");
            return;
        }

        var ids = CalcSelect();
        if (ids == "") {
            notice("您未选择任何需要彻底删除的数据", "warning");
        }
        else {

            bootbox.dialog({
                message: "<p><h4>您确定要<span class='font-red'>彻底删除</span>当前选中的内容吗 ? </h4><h5 class='font-red bold'>彻底删除后无法恢复 , 请谨慎操作</h5></p>",
                title: appConfig.toastrTitle,
                buttons: {
                    comfirm: {
                        label: "确定",
                        className: "green",
                        callback: function () {
                            var data = { ids: ids, table: GetQs("t"), classid: GetQs("classid") };
                            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "RealDels", data, function (f, d) {
                                Btn_UnDisabled(_this);
                                if (f) {
                                    //请求成功
                                    if (parseInt(d.result) > 0) {
                                        notice("操作成功", "success");
                                        window.location.href = window.location.href;
                                    }
                                    else if (parseInt(d.result) == -99) {
                                        alert("登录超时 , 请重新登录", function () {
                                            setTimeout(function () {
                                                JumpLogin();
                                            }, 500);
                                        });
                                    }
                                    else if (parseInt(d.result) == -100) {
                                        alert("权限不足 , 无法操作 !");
                                    }
                                    else {
                                        alert("操作失败 , 请稍后重试 !");
                                    }
                                }
                                else {
                                    //请求失败
                                    alert("请求出错 , 请稍后刷新重试 !");
                                }
                            });
                        }
                    },
                    cancel: {
                        label: "取消",
                        className: "default",
                        callback: function () { }
                    }
                }
            });

        }
    });
    //多个数据恢复 恢复
    $(".restores").on("click", function () {
        //判断是否回收站
        if (!IsRecycle()) {
            alert("仅在回收站中才可进行此操作");
            return;
        }

        var ids = CalcSelect();
        if (ids == "") {
            notice("您未选择任何需要恢复的数据", "warning");
        }
        else {
            
            bootbox.dialog({
                message: "<p><h4>您确定要恢复当前选中的内容吗 ? </h4></p>",
                title: appConfig.toastrTitle,
                buttons: {
                    comfirm: {
                        label: "确定",
                        className: "green",
                        callback: function () {
                            var data = { ids: ids, table: GetQs("t"), classid: GetQs("classid") };
                            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "Restores", data, function (f, d) {

                                if (f) {
                                    //请求成功
                                    if (parseInt(d.result) > 0) {
                                        notice("操作成功", "success");
                                        window.location.href = window.location.href;
                                    }
                                    else if (parseInt(d.result) == -99) {
                                        alert("登录超时 , 请重新登录", function () {
                                            setTimeout(function () {
                                                JumpLogin();
                                            }, 500);
                                        });
                                    }
                                    else if (parseInt(d.result) == -100) {
                                        alert("权限不足 , 无法操作 !");
                                    }
                                    else {
                                        alert("操作失败 , 请稍后重试 !");
                                    }
                                }
                                else {
                                    //请求失败
                                    alert("请求出错 , 请稍后刷新重试 !");
                                }
                            });
                        }
                    },
                    cancel: {
                        label: "取消",
                        className: "default",
                        callback: function () { }
                    }
                }
            });

            
        }

    });


})


//排序 防止变量污染
$(function () {
    var old_sortid = 100;
    $(".sortid_field").on("focus", function () {
        old_sortid = parseInt($(this).val());
    });

    $(".sortid_field").on("blur", function () {
        var _this = $(this);
        if (parseInt(_this.val()) !== parseInt(old_sortid)) {
            //不同开始更新
            _this.attr("disabled", "disabled");
            var data = { id: _this.attr("data-id"), sortid: parseInt(_this.val()), table: GetQs("t"), classid: GetQs("classid") };
            AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "SetSortId", data, function (f, d) {
                _this.removeAttr("disabled");
                if (f) {
                    //请求成功
                    if (parseInt(d.result) > 0) {
                        notice("修改排序成功", "success");
                    }
                    else if (parseInt(d.result) == -99) {
                        alert("登录超时 , 请重新登录", function () {
                            setTimeout(function () {
                                JumpLogin();
                            }, 500);
                        });
                    }
                    else if (parseInt(d.result) == -100) {
                        alert("权限不足 , 无法操作 !");
                    }
                    else {
                        alert("修改排序失败 , 请稍后重试 !");
                    }
                }
                else {
                    //请求失败
                    alert("请求出错 , 请稍后刷新重试 !");
                }
            });
        }

    });


})

/*2017.08.07 张浩修改，箭头排序值的点击事件*/
function UpSortId(obj, channelid, sortid) {
    var parentid = $(obj).parent().parent().parent().attr("parentid");
    var prev = $(obj).parent().parent().parents().prevAll("[parentid='" + parentid + "']");
    //var index = $(obj).parent().parents().prevAll("[parentid='" + parentid + "']").length - 1;
    if (prev.length == 0) {
        alert("已经是最靠前排序了");
        return false;
    }
    var previnfo = prev.eq(0);
    var prevsortid = previnfo.find(".sortid_field").val();
    var previd = previnfo.find(".sortid_field").attr("data-id");
    $(obj).attr("disabled", "disabled");
    var data = { id: channelid, sortid: parseInt(sortid), previd: previd, prevsortid: prevsortid, table: GetQs("t") };
    AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "UpSortId", data, function (f, d) {
        $(obj).removeAttr("disabled");
        if (f) {
            //请求成功
            if (parseInt(d.result) > 0) {
                notice("操作成功", "success");
                window.location.reload()
            }
            else if (parseInt(d.result) == -99) {
                alert("登录超时 , 请重新登录", function () {
                    setTimeout(function () {
                        JumpLogin();
                    }, 500);
                });
            }
            else if (parseInt(d.result) == -100) {
                alert("权限不足 , 无法操作 !");
            }
            else {
                alert("操作失败 , 请稍后重试 !");
            }
        }
        else {
            //请求失败
            alert("请求出错 , 请稍后刷新重试 !");
        }
    });
}

function DownSortId(obj, channelid, sortid) {
    var parentid = $(obj).parent().parent().parent().attr("parentid");
    var next = $(obj).parent().parent().parents().nextAll("[parentid='" + parentid + "']");
    if (next.length == 0) {
        alert("已经是最靠后排序了");
        return false;
    }
    var nextinfo = next.eq(0);
    var nextsortid = nextinfo.find(".sortid_field").val();
    var nextid = nextinfo.find(".sortid_field").attr("data-id");
    $(obj).attr("disabled", "disabled");
    var data = { id: channelid, sortid: parseInt(sortid), nextid: nextid, nextsortid: nextsortid, table: GetQs("t") };
    AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "DownSortId", data, function (f, d) {
        $(obj).removeAttr("disabled");
        if (f) {
            //请求成功
            if (parseInt(d.result) > 0) {
                notice("操作成功", "success");
                window.location.reload()
            }
            else if (parseInt(d.result) == -99) {
                alert("登录超时 , 请重新登录", function () {
                    setTimeout(function () {
                        JumpLogin();
                    }, 500);
                });
            }
            else if (parseInt(d.result) == -100) {
                alert("权限不足 , 无法操作 !");
            }
            else {
                alert("操作失败 , 请稍后重试 !");
            }
        }
        else {
            //请求失败
            alert("请求出错 , 请稍后刷新重试 !");
        }
    });
}

//$('#isSuperAdmin').bootstrapSwitch('state'); // true || false
//$('#isSuperAdmin').bootstrapSwitch('toggleState');
//$('#isSuperAdmin').bootstrapSwitch('state', false); // true || false
//$('#isSuperAdmin').bootstrapSwitch({
//    onSwitchChange: function (event, state) {
//        if (state == true) {
//            alert(1);
//        } else {
//            alert(2);
//        }
//    }
//});

//修改管理员密码 防止变量污染
$(function () {
    
    $(".resetpwd-submit").on("click", function () {
        var _this = $(this);
        var oldpwd = $(".layer_old_pwd").val();
        var newpwd = $(".layer_new_pwd").val();
        var renewpwd = $(".layer_re_new_pwd").val();
        if (oldpwd.trim()=="") {
            alert("请输入旧密码");
            return false;
        }
        if (newpwd.trim()=="") {
            alert("请输入新密码");
            return false;
        }
        if (!VerifyPwd(newpwd)) {
            alert("密码规则不符合安全要求 请重新输入<br/> ( 8-20位 )，包含字母、数字和特殊符号");
            return false;
        }
        if (newpwd.trim()!=renewpwd) {
            alert("两次新密码输入不一致");
            return false;
        }
        
        var data = { oldpwd: rsa.Encrypt(oldpwd), newpwd: rsa.Encrypt(newpwd), table: "Administrator", classid: "" };
        Btn_Disabled(_this);
        AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "LayerSetPwd", data, function (f, d) {
            //console.log(f,d);
            Btn_UnDisabled(_this);
            if (f) {
                //请求成功
                if (parseInt(d.result) == 1) {
                    alert("密码修改成功",function(){
                        JumpLogin();
                    });
                }
                else if (parseInt(d.result) == -99) {
                    alert("登录超时 , 请重新登录", function () {
                        setTimeout(function () {
                            JumpLogin();
                        }, 500);
                    });
                }
                else if (parseInt(d.result) == -1) {
                    alert("密码修改失败 , 请稍后重试 ");
                }
                else if (parseInt(d.result) == 0) {
                    alert("旧密码不正确 , 请重新输入 <br/> 如果您忘记旧密码 , 请联系超级管理员");
                }
                else {
                    alert("操作失败 , 请稍后重试 !");
                }
            } else {
                //请求失败
                alert("请求出错 , 请稍后刷新重试 !");
            }
        });

    });
})

//重启应用程序
$(function () {
    //重启站点
    $(".restartsite").on("click", function () {
        bootbox.confirm("确定要重启站点吗 ? <br/>重启会导致正在访问站点的用户浏览不正常 , 一般持续10秒钟左右<br/>如果超过10秒后还不能正常访问 , 建议登录服务器查看<br/>重启成功后会重新登录", function (res) {
            if (res) {
                var data = { "t": Math.random() };
                AjaxSubmit(appConfig.adminPath + "ashx/ajax.ashx", "restart", data, function (f, d) {
                    if (f) {
                        alert(d.returnval, function () {
                            window.location.href = window.location.href;
                        });
                    }
                });
            }
        })
    });

    //重载数据结构
    $(".loaddbschema").on("click", function () {
        bootbox.confirm("确定要重载数据结构吗 ? <br/>此操作会稍微久一些 , 建议不要在访问高峰期操作 . <br/>如果超过5分钟后还不能正常访问 , 建议登录服务器查看", function (res) {
            if (res) {
                var data = { "t": Math.random() };
                AjaxSubmit(appConfig.adminPath + "ashx/ajax.ashx", "loaddbschema", data, function (f, d) {
                    if (f) {
                        alert(d.returnval, function () {
                            window.location.href = window.location.href;
                        });
                    }
                });
            }
        })
    });
    //删除自动保存数据
    $(".deleteautosave").on("click", function () {
        if (appConfig.AutoSave) {
            if (localStorage.getItem(AutoSave_CookieName()) == null) {
                notice("本页面无数据可删除", "success");
            }
            else {
                localStorage.removeItem(AutoSave_CookieName());
                notice("删除自动保存内容成功", "success");
            }
        } else {
            notice("自动保存处于关闭模式", "info");
        }
    });
    //导出数据
    $(".exportexcel").on("click", function () {
        var _this = $(this);
        var classid = _this.attr("data-classid");
        var curpage = _this.attr("data-curpage");
        var searchkey = _this.attr("data-searchkey");
        var model = _this.attr("data-mode");
        var hasData = $(".table-scrollable table tbody").html().trim('') != "";
        if (!hasData) {
            notice("当前无数据可导出", "info");
            return false;
        }
        
        if (model == "curpage") {
            //导出本页数据  ,  如果有搜索条件 加上搜索条件
        }
        if (model == "curkey") {
            //导出本次搜索的所有数据
            curpage = 0;
        }
        if (model == "curclass") {
            curpage = 0;
            searchkey = "";
        }
        var url = appConfig.adminPath + "ashx/ajax.ashx?action=exportexcel&ClassId=" + classid + "&curPage=" + curpage + "&title=" + searchkey;
        var a = $("<a href='" + url + "' target='_blank'>导出</a>").get(0);
        var e = document.createEvent('MouseEvents');
        e.initEvent('click', true, true);
        a.dispatchEvent(e);

    });

});


//生成SiteMap
$(function () {
    $(".createSiteMap").on("click", function () {
        var _this = $(this);
        var url = _this.attr('data-weburl');
        if (url == "" || url == null) {
            alert("请先在站点配置里设置网站域名  <a href='Page_System/System.aspx?t=Versions&action=editVersions#tab_2'>点击去设置网站域名</a>");
            return false;
        }
        bootbox.confirm("确定要生成SiteMap吗 ? <br/>生成时间和网站数据量有关  <br/> 如果站点数据量过多 , 请耐心等待", function (res) {
            if (res) {
                var data = { "t": Math.random() };
                Btn_Disabled(_this);
                _this.html(_this.attr("data-operat"));
                AjaxSubmit(appConfig.adminPath + "ashx/administrator.ashx", "CreateSiteMap", data, function (f, d) {
                    Btn_UnDisabled(_this);
                    _this.html(_this.attr("data-text"));
                    if (f) {
                        if (parseInt(d.result) == 1) {
                            notice("SiteMap 生成成功", "success");
                        }
                        else if (parseInt(d.result) == -1) {
                            alert("SiteMap 生成失败 , 请稍后重试 ");
                        }
                        else if (parseInt(d.result) == -99) {
                            alert("登录超时 , 请重新登录", function () {
                                setTimeout(function () {
                                    JumpLogin();
                                }, 500);
                            });
                        }
                        else {
                            alert("操作失败 , 请稍后重试 !");
                        }
                    }
                    else {
                        //请求失败
                        alert("请求出错 , 请稍后刷新重试 !");
                    }

                });
            }
        })
    });
});

//生成全部伪静态
function GenerateUrl() {
    $.post("/Admin/ashx/ajax.ashx?action=generateurl", function (res) { notice("操作成功！", "success"); });
}






